﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceProcess;
using System.Data.SqlClient;
using System.IO;

namespace winApp2
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }
        string Path
        {
            get
            {
                string path = new FileInfo(Application.ExecutablePath).DirectoryName;
                if (path[path.Length - 1].ToString() != "\\")
                    path += "\\";
                return path;
            }
        }
        string GetMDF()
        {
            return Path + ddlMdf.Text;
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            this.Width = 260;
            
            foreach (string file in Directory.GetFiles(Path, "*.mdf"))
            {
                ddlMdf.Items.Add(new FileInfo(file).Name);
            }

            txtSrv.Text = UITools.ReadReg("srvName", ".");
            cbxType.SelectedIndex = 0;
           
        }

        private void cbxType_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtUid.Enabled = txtPwd.Enabled = cbxType.SelectedIndex == 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connstring = "server="+txtSrv.Text+";uid="+txtUid.Text+";pwd="+txtPwd.Text+";database=";
            if (cbxType.SelectedIndex == 1)
            {
                connstring = "Data Source=" + txtSrv.Text + ";Integrated Security=True;Initial Catalog=";
            }
            DB.ConnString = connstring;
            try
            {
                DataTable dt = DB.GetTable("master", "select 1");
                UITools.Msg("连接数据库成功!");
                UITools.WriteReg("srvName", txtSrv.Text);
                button7.Enabled=btnLogin2.Enabled= btnLogin.Enabled = true;
                if (this.Width != 530)
                {
                    this.Width = 530;
                    this.Left -= (530 - 260) / 2;
                }

                RefDB();
                
            }
            catch 
            {
                UITools.Msg("连接数据库失败!");
                btnLogin2.Enabled = btnLogin.Enabled = false;
                if (this.Width != 260)
                {
                    this.Width = 260;
                    this.Left += (530 - 260) / 2;
                }
            }
        }

        private void RefDB()
        {
            DataTable dt = DB.GetTable("master", "select name from sysdatabases where dbid>3");

            ddlDB.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ddlDB.Items.Add(dt.Rows[i][0].ToString());
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Yes;
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            new Form3().ShowDialog();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            new Form4().ShowDialog();
        }

        private void ddlMdf_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlMdf.SelectedIndex == -1) return;
            string sql = "dbcc checkprimaryfile(N'" + GetMDF() + "',3)";
            DataTable dt = DB.GetTable("master",sql);
            string ldf = dt.Rows[1][3].ToString().Trim();
            ldf = ldf.Substring(ldf.LastIndexOf("\\") + 1);
            txtLdf.Text = ldf;
            sql = "dbcc checkprimaryfile(N'" + GetMDF() + "',2)";
            dt = DB.GetTable("master",sql);
            txtDBName.Text = dt.Rows[0][1].ToString().Trim();
        }
        void Msg(string msg)
        {
            MessageBox.Show(msg, "www.hahaman.cn", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (ddlMdf.SelectedIndex == -1) return;
            if (ddlMdf.Text.Length == 0) return;
            string mdf = Path + ddlMdf.Text;
            string ldf = Path + txtLdf.Text;
            string sql = string.Format("sp_attach_db '{0}','{1}','{2}'", txtDBName.Text, mdf, ldf);
            try
            {
                DataTable dt = DB.GetTable("master",sql);
                Msg("附加成功!");
                RefDB();
            }
            catch (Exception ex)
            {
                Msg("附加失败!\n" + ex.Message);

            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (ddlDB.SelectedIndex == -1) return;
            if (ddlDB.Text.Length == 0) return;
            string sql = "sp_detach_db '" + ddlDB.Text + "'";
            try
            {
                DataTable dt = DB.GetTable("master",sql);
                Msg("分离成功!");
                RefDB();
            }
            catch (Exception ex)
            {
                Msg("分离失败!\n" + ex.Message);

            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            new Form6().ShowDialog();
        }
    }
}
